
clear all
set more off, perm
capture log close

set seed 88

set maxvar 32767


use "/Volumes/klmChinaReach2/Haihan/Revising Measuring Knowledge/Cog/rv_across_level_c.dta", clear

//cd "C:\Users\msjin\OneDrive\Desktop\measurement\"

cd "/Volumes/klmChinaReach2/Haihan/Revising Measuring Knowledge/Cog"

foreach s in c{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 G8 G9 G10 G11 G12 using `s'_across_level_q1, replace

forvalues i=1/12{
	//local k=`i'+1
	forvalues j=1/12{
		if (`j'==`i') {
			local l_q11_`i'_`j'=-99
			local l_q12_`i'_`j'=-99
			local l_q13_`i'_`j'=-99
			local l_q14_`i'_`j'=-99
			local N_q1_`i'_`j'=-99
			local f_q11_`i'_`j'=-99
			local f_q12_`i'_`j'=-99
			local f_q13_`i'_`j'=-99
			local f_q14_`i'_`j'=-99
		} 
		else {
		count if ave_pass_c_`i'>=0.75 & ave_pass_c_`j'>=0.75 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local  l_q11_`i'_`j'=`r(N)'
		count  if ave_pass_c_`i'>=0.75 & ave_pass_c_`j'<0.75  & ave_pass_c_`j'>=0.5  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
	    local l_q12_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'>=0.75 & ave_pass_c_`j'<0.5   & ave_pass_c_`j'>=0.25 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q13_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'>=0.75 & ave_pass_c_`j'<0.25  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q14_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'>=0.75 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local N_q1_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q1`mm'_`i'_`j' = `l_q1`mm'_`i'_`j''/`N_q1_`i'_`j''
		}
		}
		
	}
	
	
	post `table' ("`i'") ("1") ("q11") (`l_q11_`i'_1') (`l_q11_`i'_2') (`l_q11_`i'_3') (`l_q11_`i'_4') (`l_q11_`i'_5') (`l_q11_`i'_6') (`l_q11_`i'_7') (`l_q11_`i'_8') (`l_q11_`i'_9') (`l_q11_`i'_10') (`l_q11_`i'_11') (`l_q11_`i'_12')
	post `table' ("`i'") ("1") ("q12") (`l_q12_`i'_1') (`l_q12_`i'_2') (`l_q12_`i'_3') (`l_q12_`i'_4') (`l_q12_`i'_5') (`l_q12_`i'_6') (`l_q12_`i'_7') (`l_q12_`i'_8') (`l_q12_`i'_9') (`l_q12_`i'_10') (`l_q12_`i'_11') (`l_q12_`i'_12')
	post `table' ("`i'") ("1") ("q13") (`l_q13_`i'_1') (`l_q13_`i'_2') (`l_q13_`i'_3') (`l_q13_`i'_4') (`l_q13_`i'_5') (`l_q13_`i'_6') (`l_q13_`i'_7') (`l_q13_`i'_8') (`l_q13_`i'_9') (`l_q13_`i'_10') (`l_q13_`i'_11') (`l_q13_`i'_12')
	post `table' ("`i'") ("1") ("q14") (`l_q14_`i'_1') (`l_q14_`i'_2') (`l_q14_`i'_3') (`l_q14_`i'_4') (`l_q14_`i'_5') (`l_q14_`i'_6') (`l_q14_`i'_7') (`l_q14_`i'_8') (`l_q14_`i'_9') (`l_q14_`i'_10') (`l_q14_`i'_11') (`l_q14_`i'_12')
	post `table' ("`i'") ("1") ("N") (`N_q1_`i'_1') (`N_q1_`i'_2') (`N_q1_`i'_3') (`N_q1_`i'_4') (`N_q1_`i'_5') (`N_q1_`i'_6') (`N_q1_`i'_7') (`N_q1_`i'_8') (`N_q1_`i'_9') (`N_q1_`i'_10') (`N_q1_`i'_11') (`N_q1_`i'_12')
	post `table' ("`i'") ("1") ("f11") (`f_q11_`i'_1') (`f_q11_`i'_2') (`f_q11_`i'_3') (`f_q11_`i'_4') (`f_q11_`i'_5') (`f_q11_`i'_6') (`f_q11_`i'_7') (`f_q11_`i'_8') (`f_q11_`i'_9') (`f_q11_`i'_10') (`f_q11_`i'_11') (`f_q11_`i'_12')
	post `table' ("`i'") ("1") ("f12") (`f_q12_`i'_1') (`f_q12_`i'_2') (`f_q12_`i'_3') (`f_q12_`i'_4') (`f_q12_`i'_5') (`f_q12_`i'_6') (`f_q12_`i'_7') (`f_q12_`i'_8') (`f_q12_`i'_9') (`f_q12_`i'_10') (`f_q12_`i'_11') (`f_q12_`i'_12')
	post `table' ("`i'") ("1") ("f13") (`f_q13_`i'_1') (`f_q13_`i'_2') (`f_q13_`i'_3') (`f_q13_`i'_4') (`f_q13_`i'_5') (`f_q13_`i'_6') (`f_q13_`i'_7') (`f_q13_`i'_8') (`f_q13_`i'_9') (`f_q13_`i'_10') (`f_q13_`i'_11') (`f_q13_`i'_12')
	post `table' ("`i'") ("1") ("f14") (`f_q14_`i'_1') (`f_q14_`i'_2') (`f_q14_`i'_3') (`f_q14_`i'_4') (`f_q14_`i'_5') (`f_q14_`i'_6') (`f_q14_`i'_7') (`f_q14_`i'_8') (`f_q14_`i'_9') (`f_q14_`i'_10') (`f_q14_`i'_11') (`f_q14_`i'_12')
}

postclose `table'

}

foreach s in c{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 G8 G9 G10 G11 G12 using `s'_across_level_q2, replace

forvalues i=1/12{
	//local k=`i'+1
	forvalues j=1/12{
		if (`j'==`i') {
			local l_q21_`i'_`j'=-99
			local l_q22_`i'_`j'=-99
			local l_q23_`i'_`j'=-99
			local l_q24_`i'_`j'=-99
			local N_q2_`i'_`j'=-99
			local f_q21_`i'_`j'=-99
			local f_q22_`i'_`j'=-99
			local f_q23_`i'_`j'=-99
			local f_q24_`i'_`j'=-99
		} 
		else {
		count if ave_pass_c_`i'<0.75 & ave_pass_c_`i'>= 0.5 & ave_pass_c_`j'>=0.75 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local  l_q21_`i'_`j'=`r(N)'
		count  if ave_pass_c_`i'<0.75 & ave_pass_c_`i'>= 0.5 & ave_pass_c_`j'<0.75  & ave_pass_c_`j'>=0.5  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
	    local l_q22_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'<0.75 & ave_pass_c_`i'>= 0.5  & ave_pass_c_`j'<0.5   & ave_pass_c_`j'>=0.25 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q23_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'<0.75 & ave_pass_c_`i'>= 0.5 & ave_pass_c_`j'<0.25  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q24_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'<0.75 & ave_pass_c_`i'>= 0.5  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local N_q2_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q2`mm'_`i'_`j' = `l_q2`mm'_`i'_`j''/`N_q2_`i'_`j''
		}
		}
		
	}
	
	
	post `table' ("`i'") ("2") ("q21") (`l_q21_`i'_1') (`l_q21_`i'_2') (`l_q21_`i'_3') (`l_q21_`i'_4') (`l_q21_`i'_5') (`l_q21_`i'_6') (`l_q21_`i'_7') (`l_q21_`i'_8') (`l_q21_`i'_9') (`l_q21_`i'_10') (`l_q21_`i'_11') (`l_q21_`i'_12')
	post `table' ("`i'") ("2") ("q22") (`l_q22_`i'_1') (`l_q22_`i'_2') (`l_q22_`i'_3') (`l_q22_`i'_4') (`l_q22_`i'_5') (`l_q22_`i'_6') (`l_q22_`i'_7') (`l_q22_`i'_8') (`l_q22_`i'_9') (`l_q22_`i'_10') (`l_q22_`i'_11') (`l_q22_`i'_12')
	post `table' ("`i'") ("2") ("q23") (`l_q23_`i'_1') (`l_q23_`i'_2') (`l_q23_`i'_3') (`l_q23_`i'_4') (`l_q23_`i'_5') (`l_q23_`i'_6') (`l_q23_`i'_7') (`l_q23_`i'_8') (`l_q23_`i'_9') (`l_q23_`i'_10') (`l_q23_`i'_11') (`l_q23_`i'_12')
	post `table' ("`i'") ("2") ("q24") (`l_q24_`i'_1') (`l_q24_`i'_2') (`l_q24_`i'_3') (`l_q24_`i'_4') (`l_q24_`i'_5') (`l_q24_`i'_6') (`l_q24_`i'_7') (`l_q24_`i'_8') (`l_q24_`i'_9') (`l_q24_`i'_10') (`l_q24_`i'_11') (`l_q24_`i'_12')
	post `table' ("`i'") ("2") ("N") (`N_q2_`i'_1') (`N_q2_`i'_2') (`N_q2_`i'_3') (`N_q2_`i'_4') (`N_q2_`i'_5') (`N_q2_`i'_6') (`N_q2_`i'_7') (`N_q2_`i'_8') (`N_q2_`i'_9') (`N_q2_`i'_10') (`N_q2_`i'_11') (`N_q2_`i'_12')
	post `table' ("`i'") ("2") ("f21") (`f_q21_`i'_1') (`f_q21_`i'_2') (`f_q21_`i'_3') (`f_q21_`i'_4') (`f_q21_`i'_5') (`f_q21_`i'_6') (`f_q21_`i'_7') (`f_q21_`i'_8') (`f_q21_`i'_9') (`f_q21_`i'_10') (`f_q21_`i'_11') (`f_q21_`i'_12')
	post `table' ("`i'") ("2") ("f22") (`f_q22_`i'_1') (`f_q22_`i'_2') (`f_q22_`i'_3') (`f_q22_`i'_4') (`f_q22_`i'_5') (`f_q22_`i'_6') (`f_q22_`i'_7') (`f_q22_`i'_8') (`f_q22_`i'_9') (`f_q22_`i'_10') (`f_q22_`i'_11') (`f_q22_`i'_12')
	post `table' ("`i'") ("2") ("f23") (`f_q23_`i'_1') (`f_q23_`i'_2') (`f_q23_`i'_3') (`f_q23_`i'_4') (`f_q23_`i'_5') (`f_q23_`i'_6') (`f_q23_`i'_7') (`f_q23_`i'_8') (`f_q23_`i'_9') (`f_q23_`i'_10') (`f_q23_`i'_11') (`f_q23_`i'_12')
	post `table' ("`i'") ("2") ("f24") (`f_q24_`i'_1') (`f_q24_`i'_2') (`f_q24_`i'_3') (`f_q24_`i'_4') (`f_q24_`i'_5') (`f_q24_`i'_6') (`f_q24_`i'_7') (`f_q24_`i'_8') (`f_q24_`i'_9') (`f_q24_`i'_10') (`f_q24_`i'_11') (`f_q24_`i'_12')
}

postclose `table'

}

foreach s in c{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 G8 G9 G10 G11 G12 using `s'_across_level_q3, replace

forvalues i=1/12{
	//local k=`i'+1
	forvalues j=1/12{
		if (`j'==`i') {
			local l_q31_`i'_`j'=-99
			local l_q32_`i'_`j'=-99
			local l_q33_`i'_`j'=-99
			local l_q34_`i'_`j'=-99
			local N_q3_`i'_`j'=-99
			local f_q31_`i'_`j'=-99
			local f_q32_`i'_`j'=-99
			local f_q33_`i'_`j'=-99
			local f_q34_`i'_`j'=-99
		} 
		else {
		count if ave_pass_c_`i' <0.5 & ave_pass_c_`i' >=0.25 & ave_pass_c_`j'>=0.75 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local  l_q31_`i'_`j'=`r(N)'
		count  if ave_pass_c_`i' <0.5 & ave_pass_c_`i' >=0.25 & ave_pass_c_`j'<0.75  & ave_pass_c_`j'>=0.5  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
	    local l_q32_`i'_`j'=`r(N)'
		count if ave_pass_c_`i' <0.5 & ave_pass_c_`i' >=0.25 & ave_pass_c_`j'<0.5   & ave_pass_c_`j'>=0.25 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q33_`i'_`j'=`r(N)'
		count if ave_pass_c_`i' <0.5 & ave_pass_c_`i' >=0.25 & ave_pass_c_`j'<0.25  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q34_`i'_`j'=`r(N)'
		count if ave_pass_c_`i' <0.5 & ave_pass_c_`i' >=0.25 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local N_q3_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q3`mm'_`i'_`j' = `l_q3`mm'_`i'_`j''/`N_q3_`i'_`j''
		}
		}
		
	}
	
	
	post `table' ("`i'") ("3") ("q31") (`l_q31_`i'_1') (`l_q31_`i'_2') (`l_q31_`i'_3') (`l_q31_`i'_4') (`l_q31_`i'_5') (`l_q31_`i'_6') (`l_q31_`i'_7') (`l_q31_`i'_8') (`l_q31_`i'_9') (`l_q31_`i'_10') (`l_q31_`i'_11') (`l_q31_`i'_12')
	post `table' ("`i'") ("3") ("q32") (`l_q32_`i'_1') (`l_q32_`i'_2') (`l_q32_`i'_3') (`l_q32_`i'_4') (`l_q32_`i'_5') (`l_q32_`i'_6') (`l_q32_`i'_7') (`l_q32_`i'_8') (`l_q32_`i'_9') (`l_q32_`i'_10') (`l_q32_`i'_11') (`l_q32_`i'_12')
	post `table' ("`i'") ("3") ("q33") (`l_q33_`i'_1') (`l_q33_`i'_2') (`l_q33_`i'_3') (`l_q33_`i'_4') (`l_q33_`i'_5') (`l_q33_`i'_6') (`l_q33_`i'_7') (`l_q33_`i'_8') (`l_q33_`i'_9') (`l_q33_`i'_10') (`l_q33_`i'_11') (`l_q33_`i'_12')
	post `table' ("`i'") ("3") ("q34") (`l_q34_`i'_1') (`l_q34_`i'_2') (`l_q34_`i'_3') (`l_q34_`i'_4') (`l_q34_`i'_5') (`l_q34_`i'_6') (`l_q34_`i'_7') (`l_q34_`i'_8') (`l_q34_`i'_9') (`l_q34_`i'_10') (`l_q34_`i'_11') (`l_q34_`i'_12')
	post `table' ("`i'") ("3") ("N") (`N_q3_`i'_1') (`N_q3_`i'_2') (`N_q3_`i'_3') (`N_q3_`i'_4') (`N_q3_`i'_5') (`N_q3_`i'_6') (`N_q3_`i'_7') (`N_q3_`i'_8') (`N_q3_`i'_9') (`N_q3_`i'_10') (`N_q3_`i'_11') (`N_q3_`i'_12')
	post `table' ("`i'") ("3") ("f31") (`f_q31_`i'_1') (`f_q31_`i'_2') (`f_q31_`i'_3') (`f_q31_`i'_4') (`f_q31_`i'_5') (`f_q31_`i'_6') (`f_q31_`i'_7') (`f_q31_`i'_8') (`f_q31_`i'_9') (`f_q31_`i'_10') (`f_q31_`i'_11') (`f_q31_`i'_12')
	post `table' ("`i'") ("3") ("f32") (`f_q32_`i'_1') (`f_q32_`i'_2') (`f_q32_`i'_3') (`f_q32_`i'_4') (`f_q32_`i'_5') (`f_q32_`i'_6') (`f_q32_`i'_7') (`f_q32_`i'_8') (`f_q32_`i'_9') (`f_q32_`i'_10') (`f_q32_`i'_11') (`f_q32_`i'_12')
	post `table' ("`i'") ("3") ("f33") (`f_q33_`i'_1') (`f_q33_`i'_2') (`f_q33_`i'_3') (`f_q33_`i'_4') (`f_q33_`i'_5') (`f_q33_`i'_6') (`f_q33_`i'_7') (`f_q33_`i'_8') (`f_q33_`i'_9') (`f_q33_`i'_10') (`f_q33_`i'_11') (`f_q33_`i'_12')
	post `table' ("`i'") ("3") ("f34") (`f_q34_`i'_1') (`f_q34_`i'_2') (`f_q34_`i'_3') (`f_q34_`i'_4') (`f_q34_`i'_5') (`f_q34_`i'_6') (`f_q34_`i'_7') (`f_q34_`i'_8') (`f_q34_`i'_9') (`f_q34_`i'_10') (`f_q34_`i'_11') (`f_q34_`i'_12')
}

postclose `table'

}

foreach s in c{
tempfile `s'_across_level
tempname table
postfile `table' str8(Lang) str8(quantile) str8(Category) G1 G2 G3 G4 G5 G6 G7 G8 G9 G10 G11 G12 using `s'_across_level_q4, replace

forvalues i=1/12{
	//local k=`i'+1
	forvalues j=1/12{
		if (`j'==`i') {
			local l_q41_`i'_`j'=-99
			local l_q42_`i'_`j'=-99
			local l_q43_`i'_`j'=-99
			local l_q44_`i'_`j'=-99
			local N_q4_`i'_`j'=-99
			local f_q41_`i'_`j'=-99
			local f_q42_`i'_`j'=-99
			local f_q43_`i'_`j'=-99
			local f_q44_`i'_`j'=-99
		} 
		else {
		count if ave_pass_c_`i'<0.25 & ave_pass_c_`j'>=0.75 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local  l_q41_`i'_`j'=`r(N)'
		count  if ave_pass_c_`i'<0.25 & ave_pass_c_`j'<0.75  & ave_pass_c_`j'>=0.5  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
	    local l_q42_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'<0.25 & ave_pass_c_`j'<0.5   & ave_pass_c_`j'>=0.25 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q43_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'<0.25 & ave_pass_c_`j'<0.25  & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local l_q44_`i'_`j'=`r(N)'
		count if ave_pass_c_`i'<0.25 & ave_pass_c_`i'!=. & ave_pass_c_`j'!=.
		local N_q4_`i'_`j'=`r(N)'
		forvalues mm = 1/4 {
			local f_q4`mm'_`i'_`j' = `l_q4`mm'_`i'_`j''/`N_q4_`i'_`j''
		}
		}
		
	}
	
	
	post `table' ("`i'") ("4") ("q41") (`l_q41_`i'_1') (`l_q41_`i'_2') (`l_q41_`i'_3') (`l_q41_`i'_4') (`l_q41_`i'_5') (`l_q41_`i'_6') (`l_q41_`i'_7') (`l_q41_`i'_8') (`l_q41_`i'_9') (`l_q41_`i'_10') (`l_q41_`i'_11') (`l_q41_`i'_12')
	post `table' ("`i'") ("4") ("q42") (`l_q42_`i'_1') (`l_q42_`i'_2') (`l_q42_`i'_3') (`l_q42_`i'_4') (`l_q42_`i'_5') (`l_q42_`i'_6') (`l_q42_`i'_7') (`l_q42_`i'_8') (`l_q42_`i'_9') (`l_q42_`i'_10') (`l_q42_`i'_11') (`l_q42_`i'_12')
	post `table' ("`i'") ("4") ("q43") (`l_q43_`i'_1') (`l_q43_`i'_2') (`l_q43_`i'_3') (`l_q43_`i'_4') (`l_q43_`i'_5') (`l_q43_`i'_6') (`l_q43_`i'_7') (`l_q43_`i'_8') (`l_q43_`i'_9') (`l_q43_`i'_10') (`l_q43_`i'_11') (`l_q43_`i'_12')
	post `table' ("`i'") ("4") ("q44") (`l_q44_`i'_1') (`l_q44_`i'_2') (`l_q44_`i'_3') (`l_q44_`i'_4') (`l_q44_`i'_5') (`l_q44_`i'_6') (`l_q44_`i'_7') (`l_q44_`i'_8') (`l_q44_`i'_9') (`l_q44_`i'_10') (`l_q44_`i'_11') (`l_q44_`i'_12')
	post `table' ("`i'") ("4") ("N") (`N_q4_`i'_1') (`N_q4_`i'_2') (`N_q4_`i'_3') (`N_q4_`i'_4') (`N_q4_`i'_5') (`N_q4_`i'_6') (`N_q4_`i'_7') (`N_q4_`i'_8') (`N_q4_`i'_9') (`N_q4_`i'_10') (`N_q4_`i'_11') (`N_q4_`i'_12')
	post `table' ("`i'") ("4") ("f41") (`f_q41_`i'_1') (`f_q41_`i'_2') (`f_q41_`i'_3') (`f_q41_`i'_4') (`f_q41_`i'_5') (`f_q41_`i'_6') (`f_q41_`i'_7') (`f_q41_`i'_8') (`f_q41_`i'_9') (`f_q41_`i'_10') (`f_q41_`i'_11') (`f_q41_`i'_12')
	post `table' ("`i'") ("4") ("f42") (`f_q42_`i'_1') (`f_q42_`i'_2') (`f_q42_`i'_3') (`f_q42_`i'_4') (`f_q42_`i'_5') (`f_q42_`i'_6') (`f_q42_`i'_7') (`f_q42_`i'_8') (`f_q42_`i'_9') (`f_q42_`i'_10') (`f_q42_`i'_11') (`f_q42_`i'_12')
	post `table' ("`i'") ("4") ("f43") (`f_q43_`i'_1') (`f_q43_`i'_2') (`f_q43_`i'_3') (`f_q43_`i'_4') (`f_q43_`i'_5') (`f_q43_`i'_6') (`f_q43_`i'_7') (`f_q43_`i'_8') (`f_q43_`i'_9') (`f_q43_`i'_10') (`f_q43_`i'_11') (`f_q43_`i'_12')
	post `table' ("`i'") ("4") ("f44") (`f_q44_`i'_1') (`f_q44_`i'_2') (`f_q44_`i'_3') (`f_q44_`i'_4') (`f_q44_`i'_5') (`f_q44_`i'_6') (`f_q44_`i'_7') (`f_q44_`i'_8') (`f_q44_`i'_9') (`f_q44_`i'_10') (`f_q44_`i'_11') (`f_q44_`i'_12')
}

postclose `table'

}
